package com.mall.db.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.AdminMapper;
import com.mall.pojo.UmsAdmin;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class AdminApiImpl implements AdminApi {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 根据用户名查询管理员信息
     *
     * @param username
     * @return
     */
    @Override
    public UmsAdmin findAdminByUsername(String username) {
        LambdaQueryWrapper<UmsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdmin::getUsername, username);
        return adminMapper.selectOne(wrapper);
    }

    /**
     * 根据id查询管理员信息
     *
     * @param id
     * @return
     */
    @Override
    public UmsAdmin findById(Long id) {
        LambdaQueryWrapper<UmsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsAdmin::getId, id);
        return adminMapper.selectOne(wrapper);
    }

    /**
     * 修改管理员信息
     *
     * @param umsAdmin
     */
    @Override
    public Integer update(UmsAdmin umsAdmin) {
        return adminMapper.updateById(umsAdmin);
    }

    /**
     * 根据用户名或姓名获取用户列表分页信息
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public Page<UmsAdmin> list(Integer pageNum, Integer pageSize, String keyword) {
        Page<UmsAdmin> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(keyword),UmsAdmin::getUsername,keyword)
                .or().like(ObjectUtil.isNotEmpty(keyword),UmsAdmin::getNickName,keyword);
        adminMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 注册
     * @param umsAdminParam
     */
    @Override
    public Long register(UmsAdmin umsAdminParam) {
        adminMapper.insert(umsAdminParam);
        return umsAdminParam.getId();
    }

    /**
     * 删除指定用户信息
     * @param id
     * @return
     */
    @Override
    public Integer delete(Integer id) {
        return adminMapper.deleteById(id);
    }
}
